﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileCopy
{
    
    //移动结果
    class MoveStatus
    {
        public string status;
        public int done;
        public int total;
        public string old_path;
        public string new_path;
        public string message;
        public MoveStatus(string _status,int _done,int _total,string _old_path,string _new_path,string _message)
        {
            status = _status;
            done = _done;
            total = _total;
            old_path = _old_path;
            new_path = _new_path;
            message = _message;
        }
    }

    //移动类
    class MoveFile
    {
        public const int MOVE_BY_PREFIX = 1;
        public const int MOVE_BY_CREATE_DATE = 2;
        public const int MOVE_BY_MODIFY_DATE = 3;
        mainForm myForm;
        int type;
        string path;
        string ext;
        int length;
        string dateFormat;
        MoveStatus status;
        delegate void SetTextCallback(MoveStatus status);//设置委托
        //初始化
        public MoveFile(mainForm _myForm,int _type,string _path,string _ext,int _length,string _date_format)
        {
            myForm = _myForm;
            type = _type;
            path = _path;
            ext = _ext;
            length = _length;
            dateFormat = _date_format;
        }
        //根据文件名前几位字符重命名
        public void Move()
        {
            try
            {
                DirectoryInfo root = new DirectoryInfo(path);
                FileInfo[] files = root.GetFiles();
                int total = files.Length;
                int done = 0;
                int fail = 0;
                foreach (FileInfo f in files)
                {
                    if (f.Extension == ext)
                    {
                        string sub = "";//子目录
                        switch (type) 
                        {
                            case MOVE_BY_PREFIX:
                                sub = f.Name.Substring(0, length).ToUpper();
                                break;
                            case MOVE_BY_CREATE_DATE:
                                sub = f.CreationTime.ToString(dateFormat);
                                break;
                            case MOVE_BY_MODIFY_DATE:
                                sub = f.LastWriteTime.ToString(dateFormat);
                                break;
                            default:
                                sub = f.Name.Substring(0, length).ToUpper();
                                break;
                        }
                        string sub_path = path + "\\" + sub;
                        string message = "";
                        if (!Directory.Exists(sub_path))
                        {
                            Directory.CreateDirectory(sub_path);
                        }
                        string new_path = sub_path + "\\" + f.Name;
                        string old_path = f.FullName;
                        //看看文件是否存在
                        if(!File.Exists(new_path))
                        {
                            File.Move(old_path, new_path);
                            message = "成功";
                        }
                        else 
                        {
                            message = "同名文件已存在！";
                            fail++;
                        }

                        done++;
                        status = new MoveStatus("正在迁移！", done, total, old_path, new_path, message);
                        setWorkStatus(status);
                    }
                }
                status = new MoveStatus(done + "个符合条件文件全部迁移成功！", 0, 0, "", "", "");
                setWorkStatus(status);

            }
            catch
            {
                status = new MoveStatus("迁移出错！", 0, 0, "", "", "");
                setWorkStatus(status);
            }      
            
        }
        //设置工作状态
        public void setWorkStatus(MoveStatus status)
        {
            //来设置工作状态，解决多线程下不能设置问题
            try
            {
                if (myForm.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(setWorkStatus);
                    myForm.Invoke(d, new object[] { status});

                }
                else
                {
                    if (status.done == 0)
                    {
                        myForm.tsslStatus.Text = status.status;
                    }
                    else
                    {
                        myForm.dgvMoveResult.Rows.Add(status.old_path,status.new_path,status.message);
                        myForm.tsslStatus.Text = status + "完成" + status.done + "/" + status.total + "。";
                    }

                }
            }
            catch (Exception ex)
            {
                string info = ex.Message + ex.Data;
                MessageBox.Show(info);
            }

        }
    }
}
